<template>
	<view class="my">
		<view class="my-top-box">
			<view class="my-top">
				<u-navbar leftIcon=" " :fixed="true" :placeholder="true"  :bgColor="bgColor"/>
			</view>
		</view>
		<!-- 退出登录 -->
		<!-- <view class="loginbtn">
			<u-button type="primary" :plain="true" :hairline="true" text="退出登录" v-if="userMobile" @click="loginOut"></u-button>
			<u-button type="primary" :plain="true" :hairline="true" text="登录" v-else @click="openPage"></u-button>
		</view> -->
		<Login  ref="login"/>
		<Contact  ref="contact"/>
	</view>
</template>

<script>
	import {mapState} from 'vuex';
	import {dataRemoval} from '@/common/util.js';
	import Contact from '@/components/Contact';
	import Login from '@/components/Login';
	export default {
		computed: {
			...mapState(['userMobile','userInfo','configDetail']), // vuex获取用户数据
		},
		components: {
			Login,
			Contact
		},
		data() {
			return {
				codeImg:'',//预约码
				navIndex:4,
				userinfo:null,
				bgColor:'transparent',
				noReadNum:0,//未读数量
				dataRemoval:dataRemoval,
			}
		},
		onPageScroll(e){
			if(e.scrollTop>5){
				this.bgColor = '#FFF';
			}else{
				this.bgColor = 'transparent';
			}
		},
		onLoad(options) {
			//结束上一次监听
			// uni.$off();
			//全局事件订阅，只要订阅了事件就可以收到值
			uni.$on("loginSuccess", async (res) => {
				this.getUserinfo();
			})
			uni.$on("userInfo", async (res) => {
				this.getUserinfo();
			})
			if(this.userMobile){
				this.getUserinfo();
			}
			// 监听通知已读变化
			// uni.$on("readNum", async (res) => {
			// 	this.getNoReadNum();
			// })
		},
		onShow(){
			
		},
		methods: {
			// 退出登录
			loginOut(){
				let that = this;
				// 打开登录
				if(!this.userMobile){
					this.$refs.login.open();
					return;
				}
				uni.showModal({
					content: '确认退出登录吗？',
					confirmColor:'#0E54A4',
					success: function (res) {
						if (res.confirm) {
							that.$M.toast('退出成功')
							that.$store.commit('userToken','')
							that.$store.commit('userInfo','')
							that.$store.commit('userMobile','')
							uni.removeStorageSync('userToken');
							uni.removeStorageSync('userInfo');
							uni.removeStorageSync('userMobile');
							uni.navigateBack();
						} else if (res.cancel) {
							
						}
					}
				});
			},
			navBtn(e){
				let url = e.currentTarget.dataset.url;
				let type = e.currentTarget.dataset.type;
				let powre = this.userInfo.is_staff == 1 ? this.userInfo.staff.staff.power : [];
				// 打开登录
				if(!this.userMobile){
					this.$refs.login.open();
					return;
				}
				// 判断权限
				if(!((type == "staff" && (powre.indexOf('create_staff') != -1 || powre.indexOf('view_staff') != -1)) || (powre.indexOf(type) != -1))){
					uni.showToast({
						icon:"none",
						title:"未开通权限"
					})
					return;
				}
				uni.navigateTo({
					url: url
				})
			},
			openPage(url){
				// 打开登录
				if(!this.userMobile){
					this.$refs.login.open();
					return;
				}
				uni.navigateTo({
					url: url
				})
			},
			
			// 获取用户信息
			getUserinfo(){
				this.$H.post('api/user/getUserInfo', {}).then(res => {
					uni.stopPullDownRefresh();
					this.userinfo = res;
					this.$store.commit('userInfo', res);
				},rej=>{
					uni.stopPullDownRefresh();
				});
			},
			onReachBottom() {
				
			},
			onPullDownRefresh: function () {
				// 显⽰顶部刷新图标
				// wx.showNavigationBarLoading();
				// 隐藏导航栏加载框
				// wx.hideNavigationBarLoading();
				if(this.userMobile){
					this.getUserinfo();
				}else{
					uni.stopPullDownRefresh();
				}
			},
		}
	}
</script>
<style>
	page{
		background-color: #F4F4F4;
	}
</style>
<style lang="scss" scoped>
	.my{
		.my-top-box{
			background: linear-gradient(180deg, #0071F2 0%, #0E54A4 100%);
			padding-bottom: 80rpx;
		}
		// 头像
		.my-head{
			padding: 40rpx 24rpx 50rpx;
			.img{
				width: 120rpx;
				height: 120rpx;
				background: #FFFFFF;
				margin-right: 24rpx;
				border-radius: 50%;
				image{
					display: block;
					width: 100%;
					height: 100%;
					border-radius: 50%;
				}
				position: relative;
			}
			.tag{
				min-width: 94rpx;
				max-width: 130rpx;
				box-sizing: border-box;
				height: 32rpx;
				background: #FFFFFF;
				border-radius: 18rpx;
				display: inline-flex;
				align-items: center;
				justify-content: center;
				border: 1rpx solid #E95117;
				font-size: 18rpx;
				font-family: PingFang SC-Bold, PingFang SC;
				color: #E95117;
				position: absolute;
				z-index: 2;
				left: 50%;
				padding: 0 10rpx;
				white-space: nowrap;
				transform: translate(-50%, 0);
				// margin-left: -47rpx;
				bottom: -14rpx;
			}
			background: url("../../static/icon/arr2.png") no-repeat right 20rpx center;
			background-size: 36rpx;
			.name{
				font-size: 40rpx;
				max-width: 540rpx;
				font-family: PingFang SC-Bold, PingFang SC;
				font-weight: bold;
				color: #FFFFFF;
				
				text{
					display: block;
				}
				.tel{
					font-size: 28rpx;
					font-family: PingFang SC-Regular, PingFang SC;
					color: #FFFFFF;
					font-weight: normal;
					margin-top: 8rpx;
				}
			}
		}
		// 列表
		.my-con{
			padding: 0 24rpx 30rpx;
			display: inline-block;
			width: 100%;
			box-sizing: border-box;
			z-index: 11;
			margin-top: -60rpx;
			.my-company{
				margin-bottom: 20rpx;
				background: #FFFFFF;
				border-radius: 16rpx;
				padding: 30rpx 28rpx;
				box-sizing: border-box;
				.company-name{
					padding-left: 52rpx;
					font-size: 32rpx;
					font-family: PingFang SC-Regular, PingFang SC;
					color: #121421;
					background: url("../../static/icon/company-icon.png") no-repeat left center;
					background-size: 40rpx;
					box-sizing: border-box;
				}
			}
			.my-nav{
				background: #FFFFFF;
				border-radius: 16rpx;
				padding: 20rpx 0 0;
			}
			.my-lis{
				border-radius: 16rpx;
				padding: 20rpx 0;
				box-sizing: border-box;
				margin-bottom: 20rpx;
				text-align: center;
				width: 33.33%;
				image{
					display: block;
					width: 56rpx;
					height: 56rpx;
					margin: 0 auto 10rpx;
				}
				.name{
					font-size: 24rpx;
					font-family: PingFang SC-Regular, PingFang SC;
					color: #676767;
				}
				.num{
					display: flex;
					justify-content: center;
					align-items: center;
					min-width: 32rpx;
					height: 32rpx;
					background: #FD6865;
					font-size: 20rpx;
					color: #FFFFFF;
					border-radius: 32rpx;
					position: absolute;
					right: 60rpx;
					top: 35rpx;
					padding: 0 4rpx;
					box-sizing: border-box;
				}
			}
		}
	}
	// 退出按钮
	.loginbtn{
		padding: 0 30rpx 30rpx;
		text{
			display: inline-block;
			line-height: 50rpx;
			text-align: center;
			border-radius: 4rpx;
		}
	}
	.code-box{
		width: 540rpx;
		// height: 653rpx;
		background: #FFFFFF;
		border-radius: 24rpx;
		padding: 20rpx;
		box-sizing: border-box;
		text-align: center;
		image{
			display: block;
			width: 500rpx;
			height: 500rpx;
		}
		.but{
			display: flex;
			justify-content: center;
			align-items: center;
			margin: 10rpx 0 20rpx;
		}
		.reset{
			display: inline-block;
			width: 48rpx;
			height: 48rpx;
			background: url("../../static/icon/resetBtn.png") no-repeat center;
			background-size: 100%;
		}
		.times{
			font-size: 24rpx;
			font-family: PingFang SC-Regular, PingFang SC;
			color: #999999;
			line-height: 48rpx;
		}
		.dec{
			font-size: 24rpx;
			font-family: PingFang SC-Regular, PingFang SC;
			color: #121421;
		}
	}
</style>
